package com.xuantu.pattern.create.factory;

/**
 * 简单工厂
 * 
 * 
 * @author fengzp
 *
 */
public class SimpleFactory {

	abstract class Operator {
		private double num1 = 0;
		private double num2 = 0;

		public double getNum1() {
			return num1;
		}

		public void setNum1(double num1) {
			this.num1 = num1;
		}

		public double getNum2() {
			return num2;
		}

		public void setNum2(double num2) {
			this.num2 = num2;
		}

		public abstract double getResult();

	}

	class AddOperator extends Operator {

		@Override
		public double getResult() {
			return getNum1() + getNum2();
		}

	}

	class MulOperator extends Operator {

		@Override
		public double getResult() {
			return getNum1() * getNum2();
		}
	}

	static class OperatorFactory {
		public static Operator createOperator(String opt) {
			SimpleFactory simpleFactory = new SimpleFactory();
			switch (opt) {
			case "+":
				return simpleFactory.new AddOperator();
			case "*":
				return simpleFactory.new MulOperator();
			default:
				throw new RuntimeException();
			}

		}
	}
	
	public static void main(String[] args) {
		Operator operator = OperatorFactory.createOperator("+");
		operator.setNum1(100);
		operator.setNum2(1);
		System.out.println(operator.getResult());
	}

}
